# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openjpeg
pkgbase=mingw-w64-${_realname}2
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}2"
pkgver=2.5.0
pkgrel=4
pkgdesc="An open source JPEG 2000 codec (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.openjpeg.org/"
license=("spdx:BSD-2-Clause")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/uclouvain/openjpeg/archive/v${pkgver}.tar.gz
        0002-cmake-files-destination.patch
        0004-mingw-disable-java.patch
        0006-openjpeg-2.1.0-stdcall-for-all-win.patch
        0007-pkgconfig-add-Cflags-private.patch)
sha256sums=('0333806d6adecc6f7a91243b2b839ff4d2053823634d4f6ed7a59bc87409122a'
            '31b2cc6824b07b705ba1c05b38904c8c4fc6ba39acefebda3e6ec85dbef9a174'
            'adade9b29e4292b642d547644e26fab6e193bbfe31d25f07d89a8c35e4d0993d'
            '44679801b740ddb75edd4b55031c5f70a02eaf4f8e5eba5f3ad1b397b2ec4aed'
            '997dedd8211fc5e666b078b69ed622dd8ab4ceb05694b3ca593e92a32c151e2e')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #
prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
   0002-cmake-files-destination.patch \
   0004-mingw-disable-java.patch

  # Required for static linking in 32 bit, adds stdcall decorations in static libs
  apply_patch_with_msg \
    0006-openjpeg-2.1.0-stdcall-for-all-win.patch \
    0007-pkgconfig-add-Cflags-private.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  msg "Build static version"
  [[ -d ${srcdir}/build-static-${MSYSTEM} ]] && rm -rf ${srcdir}/build-static-${MSYSTEM}
  mkdir ${srcdir}/build-static-${MSYSTEM}
  cd ${srcdir}/build-static-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${_extra_config[@]} \
    -DBUILD_SHARED_LIBS=OFF \
    -DBUILD_STATIC_LIBS=ON \
    -DBUILD_TESTING=OFF \
    -DCMAKE_SYSTEM_PREFIX_PATH=${MINGW_PREFIX} \
    -DBUILD_JPIP=OFF \
    -DBUILD_JAVA=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./

  msg "Build shared version"
  [[ -d ${srcdir}/build-shared-${MSYSTEM} ]] && rm -rf ${srcdir}/build-shared-${MSYSTEM}
  mkdir ${srcdir}/build-shared-${MSYSTEM}
  cd ${srcdir}/build-shared-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_SHARED_LIBRARY_NAME_WITH_VERSION=ON \
    ${_extra_config[@]} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_STATIC_LIBS=OFF \
    -DBUILD_TESTING=ON \
    -DCMAKE_SYSTEM_PREFIX_PATH=${MINGW_PREFIX} \
    -DBUILD_JPIP=ON \
    -DBUILD_JAVA=OFF \
    -DBUILD_DOC=OFF \
    -DBUILD_PKGCONFIG_FILES=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

package() {
  cd "${srcdir}/build-static-${MSYSTEM}"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  cd "${srcdir}/build-shared-${MSYSTEM}"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
